<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

class MaterialTable extends AbstractTable
{

    protected $table = 'hy_material';

    protected $id = 'material_id';

    public function find($query = array())
    {
        $select = new Select(array(
            'p' => $this->getTable()
        ));
        // $select->columns(array(''));
        $select->join(array(
            'o' => 'hy_material_category'
        ), 'o.material_category_id=p.material_category_id', array(
            'material_category_name'
        ), $select::JOIN_LEFT);
        $select->join(array(
            'g' => 'hy_product'
        ), 'g.product_id=p.product_id', array(
            'product_name'
        ), $select::JOIN_LEFT);
        
        if ($query['material_category_id']) {
            $select->where(array(
                'p.material_category_id' => $query['material_category_id']
            ));
        }
        
        if (! empty($query['material_name'])) {
            $spec = function (Where $where) use($query) {
                $where->like('p.material_name', "%" . $query['material_name'] . "%");
            };
            
            $select->where($spec);
        }
        
        $select->order('p.material_id DESC');
        return $this->fetchPaginate($select, $query);
    }

    public function findOne($id)
    {
        $rowset = $this->select(array(
            'material_id' => $id
        ));
        $row = $rowset->current();
        return $row;
    }

    public function findAll()
    {
        $rowset = $this->select();
        $result = $rowset->toArray();
        return $result;
    }
    
    public function findAllByProductId($product_id)
    {
        $select = $this->select(array(
            'product_id' => $product_id
        ));
        
        //$select->columns(array('material_name'));
        $result = $select->toArray();
        return $result;
    }

    /**
     * 监管平台
     */
    public function findAllSupervision($query = array())
    {
    
        $select = new Select(array(
            'p' => $this->getTable()
        ));
//         $select->columns(array(
//             'material_name',
//         ));
    
        $select->join(array(
            'o' => 'hy_material_category'
        ), 'o.material_category_id=p.material_category_id', array(
            'material_category_name'
        ), $select::JOIN_LEFT);
        
        $select->join(array(
            'r' => 'hy_monitor_report'
        ), 'r.material_id=p.material_id', array(
            'image'
        ), $select::JOIN_LEFT);
        
        
        $select->order('p.material_id DESC');
    
        return $this->fetchPaginate($select, $query);
    }
    
    public function create($data)
    {
        return $this->insert($data);
    }

    public function updateById($data, $id)
    {
        return $this->update($data, array(
            $this->getId() => $id
        ));
    }

    public function remove()
    {}

    public function updateItem($id, $data)
    {
        if (empty($id)) {
            throw new \Exception("id field is invalid!");
        }
        $data = array(
            'resource_category_id' => $data['resource_category_id'],
            'product_id' => $data['product_id'],
            'material_code' => $data['material_code'],
            'material_name' => $data['material_name'],
            'original' => $data['original'],
            'description' => $data['description'],
            'workflow' => $data['workflow'],
            'feature' => $data['feature'],
            'contact' => $data['contact']
        );
        return $this->update($data, array(
            'material_id' => $id
        ));
    }

    public function deleteItem($id)
    {
        if (empty($id)) {
            throw new Exception("id field is invalid!");
        }
        return $this->delete(array(
            'material_id' => $id
        ));
    }

    public function addItem($data)
    {
        $data = array(
            'resource_category_id' => $data['resource_category_id'],
            'product_id' => $data['product_id'],
            'material_code' => $data['material_code'],
            'material_name' => $data['material_name'],
            'original' => $data['original'],
            'description' => $data['description'],
            'workflow' => $data['workflow'],
            'feature' => $data['feature'],
            'contact' => $data['contact'],
            'create_time' => new date()
        );
        
        $this->insert($data);
        $id = $this->lastInsertValue;
    }

    public function getItem($id)
    {
        if (empty($id)) {
            throw new Exception("id field is invalid!");
        }
        $sql = 'SELECT * FROM `hy_material` WHERE `material_id` =:material_id';
        $statement = $this->getAdapter()->query($sql);
        $results = $statement->execute(array(
            ':material_id' => $id
        ));
        $row = $results->current();
        return $row;
    }
}